<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/7
 * Time: 15:25
 */

class MongodbHelper {
    private $_cfg =array();
    public $counts=0;
    public function __construct(){
        $this->_cfg["host"] = DXW_MONGO_HOST;
        $this->_cfg["port"] = DXW_MONGO_PORT;
        $this->_cfg["database"] = DXW_MONGO_DATABASE;
        $this->_cfg["username"] = DXW_MONGO_USER_NAME;
        $this->_cfg["password"] = DXW_MONGO_USER_PWD;
        $this->_cfg["auth_type"] = DXW_MONGO_AUTHMECHANISM;

        $this->_connStr = "mongodb://".$this->_cfg["username"].":".$this->_cfg["password"]."@".$this->_cfg["host"].":".$this->_cfg["port"]."/".$this->_cfg["database"];
        $this->manager = new \MongoDB\Driver\Manager($this->_connStr);

    }

    static $instance =null;
    public static function create(){
        if(null===self::$instance){
            self::$instance = new self;
        }
        return self::$instance;
    }


    //mongo插入
    public  function mongoInsert($data){
        // 插入数据
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->insert($data);
        $result= $this->manager->executeBulkWrite($this->_cfg["database"].'.'.$this->tableName(), $bulk);

        return $result;
    }

    //mongo修改
    public function mongoUpdate($filter,$data){
        $bulk = new MongoDB\Driver\BulkWrite;
        $bulk->update($filter, ['$set' => $data], ['multi' => false, 'upsert' => false]);

        $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);

        $result = $this->manager->executeBulkWrite($this->_cfg["database"].'.'.$this->tableName(), $bulk, $writeConcern);

        return $result;
    }

    //查询数据带分页
    public function mongofindAll($filter,$field,$sort=1,$skip=0,$limit=15){
        $options=[
            'skip'=>$skip*$limit,
            'limit'=>$limit,
            'sort'=>[$field=>$sort]
        ];
        // 查询数据
        $query = new \MongoDB\Driver\Query($filter, $options);
        $info = $this->manager->executeQuery($this->_cfg["database"].'.'.$this->tableName(), $query)->toArray();
        //查询集合总数据
        $commands = [
            'count' => $this->tableName(),
            'query' => $filter
        ];
        $command = new \MongoDB\Driver\Command($commands);
        $cursor = $this->manager->executeCommand($this->_cfg["database"], $command)->toArray();
        $count = $cursor[0]->n;//总记录数


        $list=array();
        foreach ($info as $key=>$value) {
            $list[$key]=json_decode(json_encode($value),true);
        }
        return ['list'=>$list,'count'=>$count];
    }

    //查询数据 不带分页 查询所有
    public function mongofind($filter,$field,$sort=1){
        $options=[
            'sort'=>[$field=>$sort]
        ];
        // 查询数据
        $query = new \MongoDB\Driver\Query($filter, $options);
        $info = $this->manager->executeQuery($this->_cfg["database"].'.'.$this->tableName(), $query)->toArray();


        $list=array();
        foreach ($info as $key=>$value) {
            $list[$key]=json_decode(json_encode($value),true);
        }
        return $list;
    }

    public function mongofindOne($filter){
        $options=['limit'=>1];
        // 查询数据
        $query = new \MongoDB\Driver\Query($filter, $options);
        $info = $this->manager->executeQuery($this->_cfg["database"].'.'.$this->tableName(), $query)->toArray();
        $list=array();
        foreach ($info as $key=>$value) {
            $list[$key]=json_decode(json_encode($value),true);;
        }
        if(!empty($list)){
            return $list[0];
        }else{
            return $list;
        }
    }

}